// @/api/chat.js
import request from '@/utils/request';

export function getMessage(sessionId) {
    return request.get('/llm/get', {
        params: { sessionId }
    });
}

export function sendMessage(model, prompt, sessionId, think = true) {
    return request.get('/llm/chat', {
        params: {
            model,
            prompt,
            sessionId,
            think
        }
    });
}
export function newSession() {
    return request.get('/llm/new')
}

export function listSession() {
    return request.get('/llm/list')
}
export function startStreamReply(sessionId, onMessage, onError, onComplete) {
    const eventSource = new EventSource(process.env.VUE_APP_URL + `/llm/reply?sessionId=${sessionId}`);

    eventSource.addEventListener('message', (event) => {
        if (onMessage) onMessage(event);
    });

    eventSource.addEventListener('error', (err) => {
        if (onError) onError(err);
        eventSource.close();
    });

    eventSource.addEventListener('complete', () => {
        if (onComplete) onComplete();
        eventSource.close();
    });

    return eventSource;
}